package com.coo.font
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.text.Font;
	import flash.utils.getQualifiedClassName;
	
	public class FontAsset extends EventDispatcher
	{
		
		protected var _name: String;
		protected var _linkage: String;
		protected var loader: Loader;
		
		protected var prevFontArr: Array;
		
		public function FontAsset() {}
		
		public function load( url: String, linkage: String ): void
		{
			this._linkage = linkage;
			
			loader = new Loader();
			
			var context: LoaderContext = new LoaderContext();
			context.checkPolicyFile = true;
			context.applicationDomain = ApplicationDomain.currentDomain;
			
			// 로컬에서는 작동하지 않는다.
			if( Security.sandboxType == Security.REMOTE )
			{
				context.securityDomain = SecurityDomain.currentDomain;
			}
			
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onEvent );
			loader.contentLoaderInfo.addEventListener( Event.OPEN, onEvent );
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onEvent );
			loader.contentLoaderInfo.addEventListener( HTTPStatusEvent.HTTP_STATUS, onEvent );
			loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onEvent );
			
			loader.load( new URLRequest( url ), context );
		}
		
		public function close(): void
		{
			dispose();
		}
		
		public function get name(): String
		{
			return this._name;
		}
		
		protected var _isComplete: Boolean = false;
		public function get isComplete(): Boolean
		{
			return this._isComplete;
		}
		
		protected var _isError: Boolean = false;
		public function get isError(): Boolean
		{
			return this._isError;
		}
		
		protected function dispose(): void
		{
			if( loader )
			{
				loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, onEvent );
				loader.contentLoaderInfo.removeEventListener( Event.OPEN, onEvent );
				loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, onEvent );
				loader.contentLoaderInfo.removeEventListener( HTTPStatusEvent.HTTP_STATUS, onEvent );
				loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, onEvent );
				
				try
				{
					if( loader.content )
					{
						loader.unload();
					} else {
						loader.close();
					}
				} catch ( e: Error ) {
					
				}
				
				loader = null;
				prevFontArr = null;
			}
		}
		
		protected function onEvent( e: Event ): void
		{
			if( e.type == Event.OPEN )
			{
				this.prevFontArr = Font.enumerateFonts( false );
				
			} else if( e.type == Event.COMPLETE ) {
				
				try
				{
					var className: String = getQualifiedClassName( loader.content );
					if( className == "flash.display::MovieClip" )
					{
						var fontClass: Class = loader.contentLoaderInfo.applicationDomain.getDefinition( _linkage ) as Class;
						Font.registerFont( fontClass );
					}
					
					var fontArr: Array = Font.enumerateFonts( false );
					
					for each( var font: Font in fontArr )
					{
						if( getIndexByName( font ) == -1 )
						{
							_name = font.fontName;
							break;
						}
					}
					
					trace( this, "added font name :  " , _name );
					
					
				} catch ( e: Error ) {
					
					trace(this, "error - Font.registerFont............ ", e.toString());
				}
				
				this._isComplete = true;
				dispose();
				
			} else if( e.type == IOErrorEvent.IO_ERROR ) {
				
				this._isError = true;
				dispose();
			}
			
			dispatchEvent( e );
		}
		
		protected function getIndexByName( font: Font ): int
		{
			for( var i: int = 0; i< prevFontArr.length; i++ )
			{
				var item: Font = prevFontArr[i] as Font;
				if( item.fontName == font.fontName )
				{
					return i;
				}
			}
			
			return -1;
		}

	}// class
}// package











